<?php
class CustomersController extends AdminAppController
{
    public $uses = array('Admin.Customer');

    public $paginate = array(
        'limit' => 25,
        'conditions' => array('Customer.customer_visible' => 1),
        'order' => array(
            'Customer.customer_priority' => 'desc',
        ),
    );

    public function index()
    {
        $data = $this->paginate('Customer');
     
        $this->set(compact('data'));
    }


    public function add()
    {
        if ($this->request->is('post')) {
            try {
                debug($this->request->data);
                $this->Customer->saveCustomer($this->request->data['Customer']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Customer has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Customer->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function edit($id=null)
    {
        $info = $this->Customer->findById($id);
        if ($this->request->is('get')) {
            $this->request->data = $info;
        } else {
            try {
                $isUpdate = true;
                $this->Customer->saveCustomer($this->request->data['Customer'], $isUpdate, $info['Customer']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Service has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Customer->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function delete($id=null)
    {
        if ($id == null) {
            $this->redirect('/');
        }
        $data = array(
            'id' => $id,
            'customer_visible' => 0 
        );
        $this->Customer->save($data);
        $this->redirect('/admin/customers');
    }

}
